﻿using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// StaticDataEditWindow.xaml 的交互逻辑
    /// </summary>
    public partial class StaticDataEditWindow : Window
    {
        public StaticDataEditWindow()
        {
            InitializeComponent();
        }

        private string type { get; set; }

        private string lang { get; set; }

        public void init(string type1,string lang1)
        {
            type = type1;
            lang = lang1;

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            setlang();
            loadGrid();
        }

        private void setlang()
        {
            if (lang.Equals("ug"))
            {
                this.Title = "ئۇچۇر تەھرىرلەش";
                this.FlowDirection = FlowDirection.RightToLeft;
                HintAssist.SetHint(txt_value, "قىممىتى");
                
                col_name.Header = "قىممىتى";
                btn_ok.Content = "مۇقىملاش";
                btn_del.Content = "ئۆچۈرۈش";
                btn_clear.Content = "تازىلاش";

            }
            else
            {
                this.Title = "数据编辑";
                this.FlowDirection = FlowDirection.LeftToRight;

                HintAssist.SetHint(txt_value, "数据值");

                col_name.Header = "数据值";
                btn_ok.Content = "确定";
                btn_del.Content = "删除";
                btn_clear.Content = "清除";
                


            }
        }
        SQLiteHelper sqlite = new SQLiteHelper();
        private void loadGrid()
        {
           
            DataTable data = sqlite.getData("select * from other_config where type='" + type + "' and lang='"+lang+"' ");
            grid1.ItemsSource = data.DefaultView;

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            string name = btn.Name;
            switch (name)
            {
                case "btn_ok":

                    string nid= txt_id.Text;
                    string nvalue=txt_value.Text;

                    if (string.IsNullOrEmpty(nid) || string.IsNullOrEmpty(nvalue))
                    {
                        return;
                    }

                    if (string.IsNullOrEmpty(new_id))
                    {
                        //insert
                        string nnid=sqlite.getValue("select id from other_config where id='" + nid + "' and type='"+type+"' and lang='"+lang+"'", "id");
                        if (string.IsNullOrEmpty(nnid))
                        {
                            sqlite.insert("insert into other_config (id,type,value,lang) values ('" + nid + "','" + type + "','" + nvalue + "','" + lang + "')");
                            MessageBox.Show("success");
                            loadGrid();
                        }

                    }
                    else
                    {
                        //update
                        //id can not be updated
                        sqlite.insert("update other_config set value='"+nvalue+"' where type='"+type+"' and lang='"+lang+"' and id='"+new_id+"'");
                        MessageBox.Show("success");
                        loadGrid();
                    }

                    txt_id.Text = "";
                    txt_value.Text = "";
                    new_id = null;
                    new_value = null;


                    break;
                case "btn_del":
                    
                    break;
                case "btn_clear":
                    txt_id.Text = "";
                    txt_value.Text = "";
                    new_id = null;
                    new_value = null;

                    break;

            }


        }

        string new_id { get; set; }
        string new_value { get; set; }

        private void Grid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (grid1.SelectedItem != null)
            {
                DataRowView row=(DataRowView)grid1.SelectedItem;
                new_id=row.Row.ItemArray[0].ToString();
                new_value = row.Row.ItemArray[2].ToString();

                txt_id.Text = new_id;
                txt_value.Text = new_value;


            }
        }
    }
}
